package com.dy.controller;

import com.dy.dao.ESDao;
import com.dy.entity.ESEntity;
import com.dy.entity.Order;
import com.dy.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Auther: dy
 * @Date: 2022/4/25 - 04 - 25 - 21:56
 * @Description: com.dy
 * @version: 1.0
 */
@RestController
@RequestMapping("/es")
public class ESServlet {
    @Autowired
    private ESDao esDao;

    @PostMapping("/esorder")
    public void esorder(@RequestBody Order order){
        ESEntity entity = new ESEntity();
        entity.setOrderId(order.getOrderSerial());
        entity.setOrderTime(order.getOrderTime());
        entity.setAddress(order.getAddress());
        entity.setOrderText("电影名:"+order.getCinameName()+" 售价:"+order.getOrderPrice()/order.getOrderNumber());
        esDao.save(entity);
    }

    @GetMapping("/selectOrder/{info}/{pageNum}/{pageSize}")
    public PageUtil<ESEntity> selectOrder(@PathVariable("info")String info,
                                      @PathVariable("pageNum")Integer pageNum,
                                      @PathVariable("pageSize")Integer pageSize){
        PageRequest page = PageRequest.of(pageNum-1,pageSize);
        List<ESEntity> total = esDao.findAllBy("电影", null);
        PageUtil<ESEntity> pageUtil = new PageUtil<>();
        pageUtil.setTotal(total.size());
        pageUtil.setPageNum(pageNum);
        pageUtil.setPageSize(pageSize);
        List<ESEntity> allBy = esDao.findAllBy(info, page);
        allBy.stream().forEach(r->{
            System.out.println(r);
        });
        pageUtil.setList(allBy);
        return pageUtil;
    }
}
